package com.boot.app.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot.app.base.domain.BaseRoleAppRelation;
import com.boot.app.base.mapper.BaseRoleAppRelationMapper;
import com.boot.app.base.service.IBaseRoleAppRelationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class BaseRoleAppRelationServiceImpl extends ServiceImpl<BaseRoleAppRelationMapper, BaseRoleAppRelation> implements IBaseRoleAppRelationService {

    @Override
    @Transactional
    public void saveBatchByRoleIdAndAppIds(long roleId, long[] appIds) {
        LambdaQueryWrapper<BaseRoleAppRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaseRoleAppRelation::getRoleId, roleId);
        remove(wrapper);

        if(appIds == null) return;
        List<BaseRoleAppRelation> list = new ArrayList<>();
        Arrays.stream(appIds).forEach(appId -> {
            BaseRoleAppRelation roleAppRelation = new BaseRoleAppRelation();
            roleAppRelation.setRoleId(roleId);
            roleAppRelation.setAppId(appId);
            list.add(roleAppRelation);
        });
        saveBatch(list);
    }
}
